import Mock from 'mockjs'

// 生成还款计划列表
const generateRepaymentPlans = (loanId, userId, count = 12) => {
    const list = [];
    const statusMap = {
        0: '未还',
        1: '已还',
        2: '逾期'
    };
    
    const amount = Mock.Random.integer(500, 5000);
    const principal = Mock.Random.integer(300, 4000);
    const interest = amount - principal;
    
    for (let i = 0; i < count; i++) {
        // 前3期已还，第4期逾期，其余未还
        let status = 0;
        if (i < 3) {
            status = 1;
        } else if (i === 3) {
            status = 2;
        }
        
        const repayDate = new Date();
        repayDate.setMonth(repayDate.getMonth() - 3 + i);
        
        let actualRepayTime = null;
        if (status === 1) {
            actualRepayTime = new Date(repayDate);
            actualRepayTime.setDate(actualRepayTime.getDate() - Mock.Random.integer(1, 5));
        }
        
        list.push({
            id: i + 1,
            loanId,
            userId,
            period: i + 1,
            amount,
            principal,
            interest,
            repayDate: repayDate.toISOString().split('T')[0],
            status,
            statusText: statusMap[status],
            actualRepayTime: actualRepayTime ? actualRepayTime.toISOString() : null,
            createTime: Mock.Random.datetime(),
            updateTime: Mock.Random.datetime()
        });
    }
    
    return list;
};

// 生成还款记录列表
const generateRepaymentRecords = (userId, count = 5) => {
    const list = [];
    const statusMap = {
        0: '处理中',
        1: '成功',
        2: '失败'
    };
    
    for (let i = 0; i < count; i++) {
        const status = Mock.Random.integer(0, 2);
        const amount = Mock.Random.integer(500, 5000);
        
        list.push({
            id: i + 1,
            loanId: Mock.Random.integer(1, 5),
            repaymentPlanId: Mock.Random.integer(1, 12),
            userId,
            amount,
            repayTime: Mock.Random.datetime(),
            repayType: Mock.Random.integer(0, 2),
            repayTypeText: ['银行卡', '支付宝', '微信'][Mock.Random.integer(0, 2)],
            bankCardId: Mock.Random.integer(1, 3),
            tradeNo: Mock.Random.string('number', 20),
            status,
            statusText: statusMap[status],
            createTime: Mock.Random.datetime(),
            updateTime: Mock.Random.datetime()
        });
    }
    
    return list;
};

// 模拟数据
export default {
    // 获取还款计划列表接口
    '/api/repayment-plans/loan/\\d+ get': (options) => {
        const loanId = parseInt(options.url.match(/\/api\/repayment-plans\/loan\/(\d+)/)[1]);
        return {
            code: 200,
            msg: '获取成功',
            data: generateRepaymentPlans(loanId, 1)
        };
    },
    
    // 获取用户的所有还款计划接口
    '/api/repayment-plans/user/\\d+ get': (options) => {
        const userId = parseInt(options.url.match(/\/api\/repayment-plans\/user\/(\d+)/)[1]);
        const plans = [];
        
        // 生成3个贷款的还款计划
        for (let i = 0; i < 3; i++) {
            const loanPlans = generateRepaymentPlans(i + 1, userId, Mock.Random.integer(6, 24));
            plans.push(...loanPlans);
        }
        
        return {
            code: 200,
            msg: '获取成功',
            data: plans
        };
    },
    
    // 获取还款计划详情接口
    '/api/repayment-plans/\\d+ get': (options) => {
        const id = parseInt(options.url.match(/\/api\/repayment-plans\/(\d+)/)[1]);
        const plans = generateRepaymentPlans(1, 1, 12);
        const plan = plans.find(item => item.id === id) || plans[0];
        
        return {
            code: 200,
            msg: '获取成功',
            data: plan
        };
    },
    
    // 获取还款记录列表接口
    '/api/repayment-records/user/\\d+ get': (options) => {
        const userId = parseInt(options.url.match(/\/api\/repayment-records\/user\/(\d+)/)[1]);
        return {
            code: 200,
            msg: '获取成功',
            data: generateRepaymentRecords(userId)
        };
    },
    
    // 获取还款记录详情接口
    '/api/repayment-records/\\d+ get': (options) => {
        const id = parseInt(options.url.match(/\/api\/repayment-records\/(\d+)/)[1]);
        const records = generateRepaymentRecords(1, 10);
        const record = records.find(item => item.id === id) || records[0];
        
        return {
            code: 200,
            msg: '获取成功',
            data: record
        };
    },
    
    // 还款接口
    '/api/repayment-records/repay post': (options) => {
        const data = JSON.parse(options.body);
        return {
            code: 200,
            msg: '还款成功',
            data: {
                id: Mock.Random.integer(1, 100),
                loanId: data.loanId,
                repaymentPlanId: data.repaymentPlanId,
                userId: data.userId,
                amount: data.amount,
                repayTime: Mock.Random.datetime(),
                repayType: data.repayType,
                repayTypeText: ['银行卡', '支付宝', '微信'][data.repayType],
                bankCardId: data.bankCardId,
                tradeNo: Mock.Random.string('number', 20),
                status: 1,
                statusText: '成功',
                createTime: Mock.Random.datetime(),
                updateTime: Mock.Random.datetime()
            }
        };
    }
};